#! /usr/bin/python
import math
import sys
import string
import statFuncs

class FormatData:
	dataTable		= {}
	keyTable		= {}
	colMapping		= []

	def __init__(self, colSpec, keyCol):
		self.columnSpecifier	= colSpec
		self.keyCol				= keyCol
		self.numEntries			= 0
		
		for key in self.columnSpecifier:
			self.dataTable [key]	= []
			self.colMapping.append(key)

	def getColumnSpecifier(self):
		return self.columnSpecifier

	def setColumnSpecifier(self, colSpec):
		self.columnSpecifier	= colSpec

	def addColumn(self, key, type):
		self.columnSpecifier [key]	= type
		self.dataTable [key]		= []
		self.colMapping.append(key)

	def getNumEntries(self):
		return self.numEntries
	
	def getValue(self, value, type):
		if type == 'date':
			items		= string.split(value, '/')
			return '%02d/%02d/%04d'%(int(items[0]), int(items[1]), int(items[2]))
		elif type == 'time':
			items		= string.split(value, ':')
			return '%02d:%02d:%02d'%(int(items[0]), int(items[1]), int(items[2]))
		elif type == 'float':
			return float(value) 
		elif type == 'int':
			return int(value)
		else:
			return value

	def getType(self, index):
		return self.columnSpecifier [index]

	def getDefaultValues(self, key):
		if key == 'int':
			return 0
		elif key == 'float':
			return 0.0

	def addToTable(self, items):
		keyColumn	= self.keyCol.keys()[0]
		key	= items [keyColumn]
		key	= self.getValue(key, self.keyCol [keyColumn])

		if key not in self.keyTable:
			self.keyTable [key]	= self.numEntries
			for index in range(0, len(self.colMapping)):
				colIdx			= self.colMapping [index]
				self.dataTable [colIdx].append(self.getValue(items [colIdx], self.getType(colIdx)))
			self.numEntries		= self.numEntries + 1
		else:
			for colIdx in self.colMapping:
				rowIdx			= self.keyTable [key]
				self.dataTable [colIdx][rowIdx]	= self.dataTable [colIdx][rowIdx] + self.getValue(items [colIdx], self.getType(colIdx))
	
	def computeResult(self, key1, key2):
		result 		= []
		for i in range(0, self.getNumEntries()):
			result.append(float(self.dataTable [key1][i] + self.dataTable [key2][i])/float(self.dataTable [key1][i] - self.dataTable [key2][i] + 3))
		
		self.addColumn('result', 'float')
		self.dataTable ['result']	= result

	def computeSDResults(self):
		stat		= statFuncs.statFuncs()
		allKeys		= self.columnSpecifier.keys()

		for key in allKeys:
			fullKey						= str(key) + '-result'
			self.addColumn(fullKey, float)
			result						= []
			mean						= stat.computeMean(self.dataTable [key])
			sd							= stat.computeSD(self.dataTable [key])
			for index in range(0, self.getNumEntries()):
				result.append(self.applyFormula(mean, sd, self.dataTable [key][index]))

			self.dataTable [fullKey]	= result

	def applyFormula(self, mean, sd, value):
		return (float(value) - float(mean))/float(sd)

	def dumpData(self, cols=None):
		fp=open('header','w')
		if cols == None:
			cols	= []
			for index in self.colMapping:
				cols.append(index)
				fp.write(str(index)+',')
			fp.close()
		
		for key in self.keyTable:
			print key,
				
			for col in cols:
				print '\t',
				print self.dataTable [col][self.keyTable[key]],
			print '\n',

	def getColumn(self, col):
		return self.dataTable [col]

if __name__ == '__main__':
	
	dateCol			= 0
	posCol			= 4
	negCol			= 6
	wordCol			= 8
	countCol		= 9

	colSpec		= {posCol:'float', negCol:'float', wordCol:'float', countCol:'float'}
	keyCol		= {dateCol:'date'}
	format		= FormatData(colSpec, keyCol)

	for line in sys.stdin:
		items	= string.split(line.strip(), '\t')
		items.append('1.0')
		format.addToTable(items)
	
	format.computeResult(posCol, negCol)
	format.computeSDResults()
	#format.dumpData([posCol, negCol, countCol])
	format.dumpData()
	'''	
	format.computeResult(posCol, negCol)
	format.computeSDResult(posCol)
	format.computeSDResult(negCol)
	format.computeSDResult(countCol)
	format.computeSDResult('result')
	format.dumpData([1,5,'5-result',7,'7-result',9,'9-result'])
	'''
